package cn.com.majiang.controller;

import cn.com.majiang.domain.Manager;
import cn.com.majiang.enums.ResultCode;
import cn.com.majiang.param.TechargeParam;
import cn.com.majiang.result.BaseResult;
import cn.com.majiang.service.ITechargerecordService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 充卡流水管理
 * Created by jiangyanxin on 2017/5/10.
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/recharge")
public class RechargeController {

    Logger logger = LoggerFactory.getLogger(RechargeController.class);
    @Autowired
    ITechargerecordService techargerecordService;

    /**
     * 搜索充卡列表
     *
     * @throws Exception
     */
    @RequestMapping(value = "/searchRechargeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult searchAccountList(String str, HttpServletRequest request) throws Exception {
        logger.info("充卡列表请求参数" + str);
        HttpSession session = request.getSession();
        Manager manager = (Manager) session.getAttribute("manager");
        if (manager == null) {
            return BaseResult.build(ResultCode.REQUEST_FAIL);
        }
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        TechargeParam param = JSON.parseObject(str, TechargeParam.class);
        if (param == null) {
            return BaseResult.build(ResultCode.REQUEST_PARAM_ERROR);
        }
        if(manager.getId() == 1){
            param.setManagerUpId("");
        }else{
            param.setManagerUpId(manager.getName());
        }
        Map<String, Object> result = techargerecordService.searchRechargeList(param);
        return BaseResult.build(result);
    }

    /**
     * 房卡汇总
     *
     * @throws Exception
     */
    @RequestMapping(value = "/totalCard", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult totalCard( HttpServletRequest request) throws Exception {
        logger.info("买卡汇总参数");
        HttpSession session = request.getSession();
        Manager manager = (Manager) session.getAttribute("manager");
        if (manager == null) {
            return BaseResult.build(ResultCode.REQUEST_FAIL);
        }
        Map result = techargerecordService.getTotalCard(manager.getName());
        result.put("actualCard",session.getAttribute("actualCard"));
        return BaseResult.build(result);
    }


}
